<template>
  <div>
    <el-form ref="ruleForm" :rules="rules" :model="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="ruleForm.name" style="width:90%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="ruleForm.code" style="width:90%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="ruleForm.manager" placeholder="请选择" style="width:90%">
          <el-option v-for="obj in employeeArr" :key="obj.id" :label="obj.username" :value="obj.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="活动形式" prop="introduce">
        <el-input v-model="ruleForm.introduce" type="textarea" style="width:90%" />
      </el-form-item>
      <el-form-item>
        <div style="text-align:right">
          <el-button type="primary" size="small" @click="hSubmit('ruleForm')">确定</el-button>
          <el-button size="small" @click="hCancel">取消</el-button>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { getEmployeeSimple } from '@/api/employees'
import { addDepartments, getDepartDetail, updateDepartments } from '@/api/department'
export default {
  props: {
    oldList: {
      type: Array,
      require: true,
      default: null
    },
    id: {
      type: String,
      require: true,
      default: null
    },
    isEdit: {
      type: Boolean,
      require: true
    }
  },
  data() {
    const validateName = (rule, value, callback) => {
      console.log('现在输入的', value)
      let arr = []
      console.log(this.isEdit)
      if (this.isEdit) {
        // 定义一个临时常量 装当前编辑的那个对象
        const temp = this.oldList.find(item => item.id === this.id)
        arr = this.oldList.filter(item => item.pid === temp.pid && item.name !== temp.name).map(item => item.name)
      } else {
        arr = this.oldList.find(item => item.id === this.id).children.map(item => item.name)
      }
      arr.includes(value) ? callback('部门名称重复') : callback()
    }
    const validateCode = (rule, value, callback) => {
      let arr = []
      console.log(this.isEdit)
      if (this.isEdit) {
        // 定义一个临时常量 装当前编辑的那个对象
        arr = this.oldList.filter(item => item.code !== value).map(item => item.code)
      } else {
        arr = this.oldList.map(item => item.code)
        console.log(arr)
      }
      arr.includes(value) ? callback('部门编码重复') : callback()
    }
    return {
      ruleForm: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 1, max: 5, message: '长度在 1 到 50 个字符', trigger: 'blur' },
          { validator: validateName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' },
          { validator: validateCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '请选择活动区域', trigger: 'change' }
        ],
        introduce: [
          { required: true, message: '请输入活动形式', trigger: 'blur' }
        ]
      },
      employeeArr: []
    }
  },
  created() {
    this.loadEmployee()
  },
  methods: {
    async loadEmployee() {
      const res = await getEmployeeSimple()
      this.employeeArr = res.data
    },
    async loadDepartments() { // 封装的添加部门
      const res = await addDepartments({ ...this.ruleForm, pid: this.id })
      console.log(res)
      if (res.data) {
        this.$message({
          message: res.message,
          type: 'success'
        })
        this.$emit('success')
      } else {
        this.$message({
          message: res.message,
          type: 'warning'
        })
      }
    },
    async loadDetail() {
      if (this.isEdit) {
        // alert('现在是编辑，要去获取详情')
        const res = await getDepartDetail(this.id)
        console.log('现在是编辑，要去获取详情', res)
        this.ruleForm = res.data
      }
    },
    async loadUpdateDepartments() {
      const res = await updateDepartments(this.ruleForm)
      console.log(res)
      this.$message({
        message: res.message,
        type: 'success'
      })
      this.$emit('close')
    },
    async hSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.isEdit ? this.loadUpdateDepartments() : this.loadDepartments()
          this.deleteFrom
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }, // 添加分类部门
    deleteFrom() {
      this.ruleForm = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      }
    },
    hCancel() {
      this.delRule()
      this.$emit('close')
    },
    delRule() {
      this.deleteFrom()
      this.$refs.ruleForm.resetFields()
    }
  }
}

</script>
